import datetime
import time


def datetime_unix_time(date_time: datetime):
    """
    日期格式转 unix 时间戳
    :param date_time:
    :return: int
    """
    return int(time.mktime(date_time.timetuple()))


def timestamp_to_datetime(timestamp: int):
    """
    unix时间戳 转datetime对象
    :param timestamp:
    :return: datetime
    """
    return datetime.datetime.fromtimestamp(timestamp)


def array_to_datetime(array: tuple, data: dict):
    """
    数字组转日期时间
    :param array:
    :param data:
    :return:
    """
    return datetime.datetime.strptime(array[0].format_map(data), array[-1])


def build_dates(end, start='', fmt='%Y-%m-%d'):
    """
    生成日期列表
    :param end:
    :param start:
    :param fmt:
    :return:
    """
    if start == "":
        return [datetime.datetime.strptime(end, fmt).strftime(fmt)]
    else:
        dates = [datetime.datetime.strptime(start, fmt).strftime(fmt)]
        start_date = datetime.datetime.strptime(start, fmt)
        end_date = datetime.datetime.strptime(end, fmt)
        while start_date < end_date:
            start_date += datetime.timedelta(days=1)
            dates.append(start_date.strftime(fmt))
        return dates


def str_to_datetime(s, fmt="%Y-%m-%d"):
    return datetime.datetime.strptime(s, fmt)


WEEk = {'0': '星期日', '1': '星期一', '2': '星期二', '3': '星期三', '4': '星期四', '5': '星期五', '6': '星期六'}
SHIFT = {"AM": "上午", "PM": "下午"}


def get_week(date_time: datetime):
    """
    获取星期
    :param date_time:
    :return:
    """
    return WEEk[date_time.strftime('%w')]


def get_shift(date_time: datetime):
    """
    获取时间段
    :param date_time:
    :return:
    """
    return SHIFT[date_time.strftime("%p")]


def date_operation_day(date_str, fmt, num):
    return (datetime.datetime.strptime(date_str, fmt) + datetime.timedelta(days=num)).strftime(fmt)


if "__main__" == __name__:
    print(build_dates("2022-5-30", "2022-1-1"))
    print('2022-01-01'[:7])
